package single;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBcon {
	private static DBcon dBcon=new DBcon();
	private  DBcon() {}//禁止其他对象不能通过new创建数据库连接
	public Connection connection=null;	//声明连接对象
	public static DBcon getDBcon() {
		if(dBcon==null) {
			dBcon = new DBcon();
		} 
		return dBcon;
	}
	public Connection getConnection() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");//驱动加载
		} catch (Exception e) {
			System.out.println("数据库驱动加载失败：："+e);
		} 
		try {
			//数据库连接
			connection=DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/liyuan?"
					+ "serverTimeZone=Aisa/shanghai&characterEncoding=utf8&useUnicode=true"
					, "root"
					, "123456");
		} catch (SQLException e) {
			System.out.println("数据库连接失败：："+e);
		}
		return connection;
	}
	
	public void closeConnection() {
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
